/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.ruslan.kolobok.screen;

import org.ruslan.kolobok.Assets;
import org.ruslan.kolobok.Settings;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MainMenuScreen implements Screen {
//	private static final int VIEWPORT_WIDTH = Gdx.graphics.getWidth();
//	private static final int VIEWPORT_HEIGHT = Gdx.graphics.getHeight();

	Stage stage;

	private GLCommon gl;

	public MainMenuScreen(Game game) {
		gl.glClearColor(1, 1, 1, 1);

		// Skin skin = new Skin(Gdx.files.internal(Assets.UI_FILE));
		// Skin skin = new Skin(Gdx.files.internal("data/button/lala.json"));
		Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));

		stage = new Stage();
		stage.addActor(new MyActor());
		// Image splashImage = new Image(Assets.barrel);
		// stage.addActor(splashImage);
		NinePatch upPatch = new NinePatch(Assets.up, 19, 18, 35, 37);
		NinePatch downPatch = new NinePatch(Assets.down, 19, 18, 35, 37);
		// Drawable up = new TextureRegionDrawable(Assets.barrel);
		// Drawable down = new TextureRegionDrawable(Assets.soundOff);
		Drawable checked = new TextureRegionDrawable(Assets.soundOn);
		Drawable up = new NinePatchDrawable(upPatch);
		Drawable down = new NinePatchDrawable(downPatch);
		// TextButtonStyle style = new TextButtonStyle(up, down, null);
		// style.font = new BitmapFont(false);
		// style.fontColor = new Color(1, 0, 0, 1);
		// Button button = new
		// TextButton("lalala sdsdf dsfd fs sd s f sd ds sf fds", style);
		final Button button1 = new TextButton("lalala1", skin);
		button1.addListener(new InputListener() {
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				System.out.println("down");
				return true;
			}

			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				System.out.println("up: (" + x + ", " + y + ")");
				MoveToAction action = new MoveToAction();
				action.setPosition(button1.getX() + 100, button1.getY() + 100);
				action.setDuration(2);
				button1.addAction(action);
			}
		});

		Button button2 = new TextButton("lalala2", skin);
		Button button3 = new TextButton("lalala3", skin);
		// Drawable imageUp = new TextureRegionDrawable(Assets.play);
		// Button button = new ImageButton(imageUp);

		// creates the table actor
		Table table = new Table(skin);
		// 100% width and 100% height on the table (fills the stage)
		table.setFillParent(true);
		// add the table to the stage
		stage.addActor(table);
		// add the welcome message with a margin-bottom of 50 units
		table.add("Welcome to Tyrian for Android!").spaceBottom(50);
		// move to the next row
		table.row();
		// add the start-game button sized 300x60 with a margin-bottom of 10
		// units
		table.add(button1).size(300f, 60f).uniform().spaceBottom(10);
		// move to the next row
		table.row();
		// add the options button in a cell similiar to the start-game button's
		// cell
		table.add(button2).uniform().fill().spaceBottom(10);
		// move to the next row
		table.row();
		// add the high-scores button in a cell similiar to the start-game
		// button's cell
		table.add(button3).uniform().fill();

		stage.addActor(table);
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, true);
	}

	@Override
	public void show() {
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
		Settings.save();
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	public class MyActor extends Actor {
		TextureRegion region;

		public MyActor() {
			region = new TextureRegion(Assets.backgroundRegion);
		}

		public void draw(SpriteBatch batch, float parentAlpha) {
			Color color = getColor();
			batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
			batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(),
					getScaleY(), getRotation());
		}
	}
}
